//
//  BCUserCenterCoinDataView.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/16.
//  Copyright © 2018年 guoshikeji. All rights reserved.
//

#import "BCUserCenterCoinDataView.h"

//币种数据视图
#import "BCUserCenterCoinDataTypeView.h"

@interface BCUserCenterCoinDataView ()

@property (nonatomic, strong) UIScrollView *dataScroll;

/**
 *btcDataView
 */
@property (nonatomic, strong) BCUserCenterCoinDataTypeView *btcDataView;

/**
 *ethDataView
 */
@property (nonatomic, strong) BCUserCenterCoinDataTypeView *ethDataView;

@end

@implementation BCUserCenterCoinDataView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupSubviews];
        //
        [self addActions];
    }
    return self;
}
- (void)setupSubviews {
    self.dataScroll = [UIScrollView new];
    self.dataScroll.scrollEnabled = false;
    [self addSubview:self.dataScroll];
    [self.dataScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.mas_equalTo(self);
        make.width.height.mas_equalTo(self);
    }];
    //
    CGFloat dataHeight = [BCUserCenterCoinDataTypeView defaultHeight];
    self.btcDataView = [[BCUserCenterCoinDataTypeView alloc] initWithCoinType:BCCoinTypeBTC];
    [self.dataScroll addSubview:self.btcDataView];
    [self.btcDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dataScroll);
        make.left.mas_equalTo(self.dataScroll);
        make.width.mas_equalTo(self.dataScroll);
        make.height.mas_equalTo(dataHeight);
    }];
    //
    self.ethDataView = [[BCUserCenterCoinDataTypeView alloc] initWithCoinType:BCCoinTypeETH];
    [self.dataScroll addSubview:self.ethDataView];
    [self.ethDataView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.dataScroll);
        make.left.mas_equalTo(self.btcDataView.mas_right);
        make.width.mas_equalTo(self.dataScroll);
        make.height.mas_equalTo(dataHeight);
    }];
    
    //
    self.btcDataView.lastProfits = @"10.0013400";
    self.btcDataView.totleProfits = @"40.1070532";
    self.btcDataView.projects = @"1";
    self.ethDataView.lastProfits = @"0.2546709";
    self.ethDataView.totleProfits = @"15.7636894";
    self.ethDataView.projects = @"2";
}

- (void)changeShowDataToCoinType:(BCCoinType)type {
    CGPoint dataScrollContentOffset = self.dataScroll.contentOffset;
    if (type==BCCoinTypeBTC) {
        dataScrollContentOffset.x = 0;
    }else if (type==BCCoinTypeETH) {
        dataScrollContentOffset.x = self.dataScroll.qmui_width;
    }
    self.dataScroll.contentOffset = dataScrollContentOffset;
    //切换动画
    CATransition *transition = [CATransition animation];
    transition.type = @"rippleEffect";
    transition.subtype = type==BCCoinTypeBTC?kCATransitionFromLeft:kCATransitionFromRight;
    transition.duration = .6;
    transition.removedOnCompletion = YES;
    transition.repeatCount = 1;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut];
    [self.dataScroll.layer removeAllAnimations];
    [self.dataScroll.layer addAnimation:transition forKey:@""];
}

- (void)layoutSubviews {
    [super layoutSubviews];
    //
    self.dataScroll.contentSize = CGSizeMake(2*self.dataScroll.frame.size.width, 0);
}

/**
 给按钮添加事件
 */
- (void)addActions {
    //last
    [self.btcDataView addLastProfitsTarget:self action:@selector(didClickedLastProfitsAction)];
    [self.ethDataView addLastProfitsTarget:self action:@selector(didClickedLastProfitsAction)];
    //totle
    [self.btcDataView addTotleProfitsTarget:self action:@selector(didClickedTotleProfitsAction)];
    [self.ethDataView addTotleProfitsTarget:self action:@selector(didClickedTotleProfitsAction)];
    //projects
    [self.btcDataView addProjectsTarget:self action:@selector(didClickedProjectsAction)];
    [self.ethDataView addProjectsTarget:self action:@selector(didClickedProjectsAction)];
}

- (void)didClickedLastProfitsAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userCenterCoinDataViewDidClickedLastProfitsAction)]) {
        [self.delegate userCenterCoinDataViewDidClickedLastProfitsAction];
    }
}
- (void)didClickedTotleProfitsAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userCenterCoinDataViewDidClickedTotleProfitsAction)]) {
        [self.delegate userCenterCoinDataViewDidClickedTotleProfitsAction];
    }
}
- (void)didClickedProjectsAction {
    if (self.delegate && [self.delegate respondsToSelector:@selector(userCenterCoinDataViewDidClickedProjectsAction)]) {
        [self.delegate userCenterCoinDataViewDidClickedProjectsAction];
    }
}



+ (CGFloat)defaultHeight {
    return [BCUserCenterCoinDataTypeView defaultHeight];
}

@end
